package blazingbubble.lobby.command.net;

import spark.math.Random;
import blazingbubble.command.NetworkCommand;
import blazingbubble.lobby.command.LoginCommand;
import spark.network.protocol.PreLogin;
import spark.network.NetworkConnection;
import spark.network.Sender;
import spark.network.IPreLoginResponseResultListener;

public class NetLoginCommand extends LoginCommand implements IPreLoginResponseResultListener {
	private NetworkCommand networkCommand;

	private int responsePort;
	private String loginName;

	public NetLoginCommand(Sender sender, NetworkConnection serverConnection) {
		networkCommand = new NetworkCommand(sender, serverConnection);
	}

	public int getResponsePort() {
		return responsePort;
	}

	public void setResponsePort(int responsePort) {
		this.responsePort = responsePort;
	}

	@Override
	public void login(String loginName) {
		this.loginName = loginName;
		PreLogin packet = new PreLogin();
		packet.setValidation(Random.getInstance().nextLong());
		packet.setResponsePort(responsePort);
		networkCommand.send(packet);
	}

	@Override
	public void preLoginFailed(String reason) {
		//TODO : auto generated stub
		throw new RuntimeException("Not implemented");
	}

	@Override
	public void preLoginSucceeded() {
		moltencore.network.protocol.Login packet = new moltencore.network.protocol.Login();
		packet.setName(loginName);
		networkCommand.send(packet);
	}
}
